package com.zb.mobile.user.controller;

import com.zb.annotation.SystemLog;
import com.zb.constant.LogConstant;
import com.zb.constant.OperationLogModuleType;
import com.zb.constant.OperationLogType;
import com.zb.entity.AccounLoginInfo;
import com.zb.exception.VendorIdentifierException;
import com.zb.mobile.UserCodeRegisterVo;
import com.zb.mobile.user.service.ConUserMobileCodeService;
import com.zb.mobile.user.service.ConUserMobileGoodsService;
import com.zb.result.PageUtil;
import com.zb.result.Result;
import com.zb.utils.TokenUserUtil;
import com.zb.vo.UserContactVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

/**
 * @Author WEN
 * @Date 2023/11/6 0006
 **/
@Api(tags = "用户报单移动端编码类接口")
@RestController
@RequestMapping("/mobile/user/code")
public class UserCodeController {
    @Resource
    private ConUserMobileCodeService conUserMobileCodeService;
    @Resource
    private ConUserMobileGoodsService conUserMobileGoodsService;


    @ApiOperation("防伪查询 - 根据编码/仓库编码查询")
    @GetMapping("/getSecurityCheck")
    public Result getSecurityCheck(HttpServletRequest request, String code) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data("row", conUserMobileCodeService.getSecurityCheck(loginInfo, code).get("id"));
    }

    @ApiOperation("防伪查询/电子保修卡 - 根据编码ID获取编码/电子保修卡详情")
    @GetMapping("/getCodeDetail/{codeId}")
    public Result getCodeDetail(@PathVariable @ApiParam(value = "编码ID", required = true) Long codeId) {
        return Result.success().data("row", conUserMobileGoodsService.getGoodsDetail(codeId));
    }

    @ApiOperation("电子保修卡 - 根据编码查询")
    @GetMapping("/getRegisterSecurityCheck")
    public Result getRegisterSecurityCheck(HttpServletRequest request, String code) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data("row", conUserMobileCodeService.getSecurityCheck(loginInfo, code));
    }

    @ApiOperation("电子保修卡 - 获取保修卡列表")
    @PostMapping("/getUserCodeList")
    public Result getUserCodeList(HttpServletRequest request, @RequestBody PageUtil pageUtil) {
        Long userId = TokenUserUtil.getAccountIdByToken(request);
        return Result.success().data(conUserMobileCodeService.getUserCodeList(userId, pageUtil));
    }

    @ApiOperation("电子保修卡 - 获取客户登记地址列表")
    @PostMapping("/getUserRegisterAddressList")
    public Result getUserRegisterAddressList(HttpServletRequest request, @RequestBody PageUtil pageUtil) {
        Long userId = TokenUserUtil.getAccountIdByToken(request);
        return Result.success().data(conUserMobileCodeService.getUserRegisterAddressList(userId, pageUtil));
    }

    @ApiOperation("获取编码登记自定义字段")
    @GetMapping("/getUserCodeRegisterField")
    public Result getUserCodeRegisterField(HttpServletRequest request) throws VendorIdentifierException {
        Long custAccId = TokenUserUtil.getcustAccIdByToken(request);
        return Result.success().data(conUserMobileCodeService.getUserCodeRegisterField(custAccId));
    }

    @SystemLog(moduleType = OperationLogModuleType.USER_MODULE_TYPE, type = OperationLogType.INSERT_UPDATE, empType = LogConstant.SYSTEM_LOG_EMP_TYPE_2, remark = "增加电子保修卡")
    @ApiOperation("登记激活产品")
    @PostMapping("/saveUserCodeRegister")
    public Result saveUserCodeRegister(HttpServletRequest request, @RequestBody @Valid UserCodeRegisterVo codeRegisterVo) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        Map<String, Object> resultMap = conUserMobileCodeService.saveUserCodeRegister(loginInfo, codeRegisterVo);
        return Result.success("激活登记成功").data(resultMap);
    }

    @ApiOperation("工单报单-扫码报单，获取编码信息")
    @GetMapping("/getUserOrderCodeInfo")
    public Result getUserOrderCodeInfo(HttpServletRequest request, @RequestParam @ApiParam(value = "编码", required = true) String code) throws VendorIdentifierException {
        AccounLoginInfo loginInfo = TokenUserUtil.getAccountLoginInfo(request);
        if (null == loginInfo) {
            loginInfo = new AccounLoginInfo(0L, "", "", "", "");
            loginInfo.setCustAccId(TokenUserUtil.getCustAccIdByMobildUserCustCode(request));
        }
        return Result.success().data("row", conUserMobileCodeService.getUserOrderCodeInfo(loginInfo, code));
    }

    @ApiOperation("获取编码扫码详情信息")
    @GetMapping("/getUserCodeDetail/{codeId}")
    public Result getUserCodeDetail(HttpServletRequest request, @PathVariable @ApiParam(value = "编码ID", required = true) Long codeId) throws VendorIdentifierException {
        Long custAccId = TokenUserUtil.getcustAccIdByToken(request);
        return Result.success().data(PageUtil.row(conUserMobileCodeService.getUserCodeDetail(custAccId, codeId)));
    }


}
